<template>
  <div class="full-page">
    <div class="login-wrap"></div>
    <div class="bg-rgba"></div>
    <div class="login-shaw"></div>  
    <div class="login-box">
      <div class="other-more">
        <img class="logo-text" src="~@/assets/img/login-text.png" />
        <img class="logo" src="~@/assets/img/logo-login.png" />
      </div>
      <div class="item flex-row">
        <i class="el-icon1-erp-icon-"></i><span class="u-t">账号</span>
        <div class="input-wrap">
          <input type="text" v-model="formLogin.username" placeholder="请输入登录账号">
        </div>
      </div>
      <div class="item flex-row">
        <i class="el-icon1-erp-mima1"></i><span class="u-t">密码</span>
        <div class="input-wrap">
          <input type="password"
                  v-model="formLogin.password"
                  @keyup.enter="login()"
                  placeholder="请输入登录密码">
        </div>
      </div>
      <el-button class="btn flex-row-ver" v-loading="isloading" @click="login()" :disabled="isloading">
        <span>登录</span>
      </el-button>
      </div>
      <div class="bottom-text">
        <div>武汉不止一点科技有限公司@All Rights Reserved</div>
        <div>鄂ICP备18016776号-1</div>
      </div>
  </div>
</template>

<script>
  import imd5 from '@/utils/md5.js'
  export default {
    data() {
      return {
        isloading: false,
        formLogin: {
          username: '',
          password: ''
        }
      }
    },
    methods: {
      login() {
        if (!this.formLogin.username){
          this.$message.error('请输入用户名')
          return false
        }
        if (!this.formLogin.password) {
          this.$message.error('请输入密码')
          return false
        }
        const params = {
          'username': this.formLogin.username,
          'password': imd5.md5(this.formLogin.password)
        }
        this.isloading = true
        this.$http.post('/server/home/login', JSON.stringify(params)).then(res => {
          if (res.data.success) {
            const r = res.data.data
            r.user = this.formLogin.username
            if (!r.remainer) {
              r.remainer = 0
            }
            sessionStorage.setItem('loginMsg', JSON.stringify(r))
            if (this.$route.query.redirect) {
              this.$router.replace({path: this.$route.query.redirect})
            } else {
              this.$router.replace({path: '/main/index/index'})
            }
          }
        }).finally(() => { this.isloading = false })
      }
    },
    created() { // 自动登录
      const loginMsg = sessionStorage.getItem('loginMsg') || '{}'
      if (loginMsg && JSON.parse(loginMsg).token) {
        if (this.$route.query.redirect) {
          this.$router.replace({path: this.$route.query.redirect})
        }else {
          this.$router.replace({path: '/main/index/index'})
        }
      }
    }
  }

</script>
<style lang="sass" scoped>
  @import './css/login.scss';
</style>
